package com.mz.ozy.activity.assets;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.mz.ozy.R;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.RechargeRecordBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.RechargeRecordPresenter;
import com.mz.ozy.mvp.view.RechargeRecordContract;
import com.mz.ozy.utils.BitmapUtils;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideCircleTransform;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import org.json.JSONObject;

import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author liucan on 2020/1/3 0003.
 */
public class PaymentActivity extends BaseActivity<RechargeRecordContract.View, RechargeRecordPresenter> implements RechargeRecordContract.View {

    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.line_view)
    LinearLayout lineView;
    @BindView(R.id.tv_invite_code)
    TextView mTvInviteCode;
    private SimpleTarget target = new SimpleTarget<Bitmap>(120, 120) {

        @Override
        public void onResourceReady(Bitmap bitmap, @Nullable Transition<? super Bitmap> glideAnimation) {
            //这里我们拿到回掉回来的bitmap，可以加载到我们想使用到的地方
            ivQrcode.setImageBitmap(CodeUtils.createImage(username, Utils.dp2px(PaymentActivity.this, 400), Utils.dp2px(PaymentActivity.this, 400), bitmap));
        }
    };
    private String username;
    private String userIcon;
    private String code;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);
        initTitle();
        tvCenterTitle.setText("收款码");
        ButterKnife.bind(this);
        code = getIntent().getStringExtra("code");
        mTvInviteCode.setText("ID " + code);
        username = SPUtils.getInstance(this).getString(Constants.SPName.USERNAME, "");
        userIcon = SPUtils.getInstance(this).getString(Constants.SPName.USERAVATAR, "");
//        mPresenter.getRecharg(this);
        setQrCode(code);
    }

    private void setQrCode(String username) {
        if (TextUtils.isEmpty(userIcon)) {
            ivQrcode.setImageBitmap(CodeUtils.createImage(username, Utils.dp2px(this, 400), Utils.dp2px(this, 400), BitmapFactory.decodeResource(getResources(), R.mipmap.kt_y)));

        } else {
            ivQrcode.setImageBitmap(CodeUtils.createImage(username, Utils.dp2px(this, 400), Utils.dp2px(this, 400), null));
            RequestOptions options = new RequestOptions()
                    .error(R.mipmap.kt_y)
                    .placeholder(R.mipmap.kt_y)
                    .centerCrop()
                    .transform(new GlideCircleTransform())
                    .diskCacheStrategy(DiskCacheStrategy.ALL);   //在load()和into()之间，可以串连添加各种功能。
            Glide.with(this).load(userIcon).apply(options).into(ivHead);
        }
    }

    @OnClick(R.id.bt_copy)
    public void onViewClicked() {
        Bitmap mBitmap1 = BitmapUtils.createBitmap1(lineView);
        if (mBitmap1 != null) {
            Uri uri = BitmapUtils.saveImageToGallery(this, mBitmap1);
            if (uri != null) {
                ToastUtils.showToast(this.getString(R.string.str_save_photo_success));
            } else {
                ToastUtils.showToast(this.getString(R.string.str_save_photo_failure));

            }
        }
    }

    @Override
    public void getRechargeRecordSuccess(RechargeRecordBean bean) {

    }

    @Override
    public void getRechargeSuccess(JSONObject bean) {
       // setQrCode(bean.getWalletAdress());
    }
}
